<?php

include_once("class.phpmailer.php");
include_once("class.smtp.php");


function send_mail($to_arr, $subject, $content)
{
    include(dirname(__FILE__) . '/config.php');
	
	$mail = new PHPMailer();
    $mail->CharSet = "UTF-8";
    $mail->IsSMTP(); // set mailer to use SMTP

    $mail->Host = $email_host; // specify main and backup server
    $mail->Port = $email_port; // set the port to use
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->SMTPSecure = 'ssl';
    
    $mail->Username = $email_user; // your SMTP username or your gmail username
    $mail->Password = $email_pass; // your SMTP password or your gmail password
    $from = $email_user; // Reply to this email
   
    $mail->From = $email_user;
    $mail->FromName = $email_name; // Name to indicate where the email came from when the recepient received
    
	foreach($to_arr as $to){
		$mail->AddAddress($to);
		 $mail->WordWrap = 50; // set word wrap
    $mail->IsHTML(true); // send as HTML
    $mail->Subject = $subject;
    $mail->Body = $content; //HTML Body
    
	//$mail->SMTPDebug = 2;
    if (!$mail->Send()) {
    
        die("<h1>Loi khi goi mail: " . $mail->ErrorInfo . '</h1>');
        //logMe($to, 0);
    } else {
        echo 'sent to ' . $to;
        //logMe($to, 1);
    }
	}
	
    
   
}

function log_mail($mail_to, $status,$day_sent,$description)
{
    $con = mysqli_connect("localhost", "root", "", "email_marketing");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sqlInsert = "INSERT INTO `log` (`email`, `sent`,`time_sent`) VALUES ('" . $mail . "', '" . $status . "'," . time() . "); ";

    mysqli_query($con, $sqlInsert);
    echo $sqlInsert;
    mysqli_close($con);
}





?>
